<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <home-main-template>
      <template slot="top" class="top"><el-button type="primary">添加角色</el-button></template>
      <template slot="center">
        <el-table :data="rolesList" border stripe>
          <!-- 展开列 -->
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row v-for="(item1) in scope.row.children" :key="item1.id">
                <!-- 一级权限 -->
                <el-col :span="5">
                  <el-tag>{{item1.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <!-- 二三级权限 -->
                <el-col :span="19">
                  <!-- 二级for循环渲染 -->
                  <el-row v-for="(item2) in item1.children" :key="item2.id">
                    <el-col :span="6">
                      <el-tag type="success">{{item1.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag v-for="(item3) in item2.children" :key="item3.id" 
                      type="warning" closable @close='removeRightById(scope.row,item3.id)'>{{item3.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
          <el-table-column label="操作">
            <template>
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
              >编辑</el-button>
              <el-button 
                type="danger"
                icon="el-icon-delete"
                size="mini"
              >删除</el-button>
              <el-button
              @click="showRightDialog(scope.row)"
                type="warning"
                icon="el-icon-setting"
                size="mini"
              >分配权限</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </home-main-template>
    <el-dialog
      title="分配权限"
      :visible.sync="setRightDialogVisible"
      width="50%"
      @close="setRightRemove"
    >
      <!-- 树形控件 -->
      <el-tree
        :data="rightsList"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defKey"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import HomeMainTemplate from "../../../../../components/content/HomeMainTemplate.vue";
import {getrolesList,removeRight,getRightAll,setUserRights} from "@/network/roles";
export default {
  components: { HomeMainTemplate },
  name: "Roles",
  data() {
      return {
          rolesList:[],
          rightsList:[],
          //树形控件的属性绑定对象
          treeProps:{
             label: 'authName',
             children: 'children'
          },
          // 默认勾选的权限
          defKey: [],
          // 当前权限id
          nowRolesId: null
      }
  },
  created() {
    this.getroles()
  },
  methods: {
      getroles(){
           getrolesList().then(
               res =>{
                   const {data ,meta} = res
                   if(meta.status !== 200) return  this.$message.error(meta.msg)
                   this.rolesList = data
               }
           )
      },
      //根据ID删除权限
      removeRightById(row, rolesId){
        //提示弹框
        this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(()=>{
          this.$message({
            type:'info',
            message:'已取消删除'
          })
        })
        removeRight(row.id, rolesId).then(
          res =>{
            const {meta} = res
                   if(meta.status !== 200) return  this.$message.error(meta.msg)
                   this.getrolesList()
          }
        )
      },
      //获取所有权限列表
      showRightDialog(row){
        this.nowRolesId = row.id
        getRightAll().then(
          res =>{
             const { data, meta } = res
             if (meta.status !== 200) return this.$mssage.error(meta.msg)
             this.rightsList = data
          }
        )
        this.getLeafKeys(row, this.defKey)
        this.setRightDialogVisible = true
      },
      // 通过递归形式,获取角色权限三级id保存到defkeys数组
      getLeafKeys(node, arr) {
        if (!node.children) {
          return arr.push(node.id)
        }
        node.children.forEach(element => {
          this.getLeafKeys(element, arr)
        })
      },
       // 清空数组内权限id
      setRightRemove() {
        this.defKey = []
      },
       //确定后角色分配权限
      allotRights(){
        const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
        const idStr = keys.join(',')
        setUserRights(this.nowRolesId,idStr).then(
          res =>{
             const {meta } = res
             if ( meta.status !== 200) return this.$msg.error(meta.msg)
             this.$msg.success(meta.msg)
             this.requestRolesList()
             this.setRightDialogVisible = false
          }
        )
      }
  },
};
</script>

<style scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-row{
  display: flex;
  align-items:center ;
}
.el-tag {
  margin-right:15px ;
}
</style>